package com.hua.yuan.live.repertory.cloud_data.loader;
import com.alibaba.fastjson.JSONObject;
import com.hua.yuan.common.https.OkHttpModule;
import com.hua.yuan.live.repertory.cloud_data.api.LiveRoomInfoApi;
import com.hua.yuan.live.repertory.cloud_data.res.ResultMsg;
import java.io.IOException;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Call;
import retrofit2.Response;

/**
 * 赠送代币
 */
public class LiveGiveMoneyLoader {

    private Disposable mPublish;


    public interface OnGivePlayCallback{

        /**
         * 购买记录
         * @param isPlay 是否已购买
         */
        void playVideoStatus(ResultMsg isPlay);

        void onFailure(Throwable e);

    }


    public static void requestGiveMoney(final String pToken, final int pAnchorId, final String money, final OnGivePlayCallback callback) {

        Observable.create(new ObservableOnSubscribe<ResultMsg>() {

            @Override
            public void subscribe(ObservableEmitter<ResultMsg> pEmitter) throws Exception {

                LiveRoomInfoApi mApi = OkHttpModule.getInstance().getRetrofit().create(LiveRoomInfoApi.class);
                Call<String> call = mApi.giveMoney(pToken,pAnchorId,money);
                try {
                    Response<String> response = call.execute();
                    if (response.body() == null || response.body().equals("")) {
                        pEmitter.onNext(null);
                        return;
                    }
                    ResultMsg resultData = JSONObject.parseObject(response.body(), ResultMsg.class);
                    if (resultData == null) {
                        pEmitter.onNext(null);
                        return;
                    }
                    pEmitter.onNext(resultData);
                    pEmitter.onComplete();
                } catch (IOException e) {
                    e.printStackTrace();
                    pEmitter.onError(e);
                }
            }

        }).subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResultMsg>() {

                    private Disposable mDisposable;

                    @Override
                    public void onSubscribe(Disposable d) {
                        mDisposable=d;
                    }

                    @Override
                    public void onNext(ResultMsg value) {

                        callback.playVideoStatus(value);
                        mDisposable.dispose();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mDisposable.dispose();
                    }

                    @Override
                    public void onComplete() {
                        mDisposable.dispose();
                    }
                });
    }
}
